<?php
/*
 * @Author: your name
 * @Date: 2021-08-04 02:12:15
 * @LastEditTime: 2022-01-13 09:34:56
 * @LastEditors: 李明
 * @Description: In User Settings Edit
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/Controller.php
 */

namespace App\Http\Controllers;

use App\Http\Controllers\common\CommonFun;
use App\Models\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Redis;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;



    public static function tokenValidityLongTime()
    {
        $timeLong = 60 * 60 * 24 * 7;
        return $timeLong;
    }



    //验证Token是否生效
    public static function verifyTokenLm($type = 'worker')
    {
        $token = Request()->header('token');
        $channel = Request()->header('channel') ?? '1';

        if (is_null($token) || $token == '') {
            $arr['code'] = 400;
            $arr['msg'] = '缺少Token';
            $arr['data'] = [];
            return json_encode($arr, JSON_UNESCAPED_UNICODE);
        }
        if ($type == 'worker') {
            $token_worker = CommonFun::GetTokenData($token);
            $keyToken_worker = CommonFun::TokenKeyCreate($token_worker['worker_id'], $channel);
            $redis_token = Redis::get($keyToken_worker);
            if ($token == $redis_token) {
                $token_worker['code'] = 200;
                $token_worker['msg'] = '校验成功';
                $token_worker['tokenIsValid'] = true;
                // 刷新有效期
                Redis::setex($keyToken_worker, self::tokenValidityLongTime(), $token);
                return json_encode($token_worker, JSON_UNESCAPED_UNICODE);
            } else {
                $arr['code'] = 400;
                $arr['msg'] = 'Token无效';
                $arr['data'] = [];
                return json_encode($arr, JSON_UNESCAPED_UNICODE);
            }
        } elseif ($type == 'user') {
            $channel = '3';
            $token_user = CommonFun::GetTokenData($token);
//            $user = User::find($token_user['user_id']);
//            if ($user->is_update_pwd != 1) {
//                $arr['code'] = 400;
//                $arr['msg'] = '请先修改密码';
//                $arr['data'] = [];
//                return json_encode($arr, JSON_UNESCAPED_UNICODE);
//            }
            $keyToken_user = CommonFun::TokenKeyCreate($token_user['user_id'], $channel);
            $redis_token = Redis::get($keyToken_user);
            if ($token == $redis_token) {
                $token_user['code'] = 200;
                $token_user['msg'] = '校验成功';
                $token_user['tokenIsValid'] = true;
                // 刷新有效期
                Redis::setex($keyToken_user, self::tokenValidityLongTime(), $token);
                return json_encode($token_user, JSON_UNESCAPED_UNICODE);
            } else {
                $arr['code'] = 400;
                $arr['msg'] = 'Token无效';
                $arr['data'] = [];
                return json_encode($arr, JSON_UNESCAPED_UNICODE);
            }
        } else {
            $arr['code'] = 400;
            $arr['msg'] = 'Token无效';
            $arr['data'] = [];
            return json_encode($arr, JSON_UNESCAPED_UNICODE);
        }
    }
}
